<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *  File : listModules.php
 *  called only via ajax 
 *  List all the modules in the table 'modules'
 */  
 

require '../../includes/classes/WS3tableview.php';
$message="";

// build an array of theme names with their ids
$themeArray=array();
$themeArray[0]='N/A';

$dbTheme=new WS3_Database();
$q="SELECT * FROM `theme` WHERE 1";


if ($dbTheme->query($q))
{
  while ( $dbTheme->nextRecord() )
  {
    $themeArray[$dbTheme->f('id')]=$dbTheme->f('name');
  } 
}


// pull the modules from the database
$db=new WS3_Database();

$q="SELECT m.id AS mid, m.name AS mname, m.active AS mactive,
t.name AS tname
FROM `module` As m, `theme` AS t
WHERE m.theme_id = t.id
ORDER BY t.name, m.name
";


if ($db->query($q))
{
  $table=new WS3_TableView('main',400);
  $table->setTitle( 'Listing Modules' );
  $table->addColumnTH('Theme');
  $table->addColumnTH('Name');

if ($session->userType() < 3) // only admin and headmaster
{  
  $table->addColumnActive(true,'module');
  $table->addColumnEdit(true,'Module');
  $table->addColumnDelete(true,'Module');
}
  
  $table->addColumn('Content','listContents');
  $table->setAlternativeRows(true);
  $table->setInfoRow(true);
  $count = $db->numberRows();
  while ( $db->nextRecord() )
  {
     $table->addRow($db->f('mid'), array(
     'Theme' => javaSafeString( $db->f('tname') ), // $themeArray[$db->f('theme_id')] ), 
     'Name' => javaSafeString( $db->f('mname') ),            
     'active' => $db->f('mactive')
      ));
  } 
  $table->addToInfoRow("<p>Count = $count </p>");
  echo $table->display(); 
  $message="Listing";
}else{
  $message="Error : Empty list";
}
 


/** Get menu */
require '../../includes/menus/topModules.php';
$menu=javaSafeString($menu);
echo "document.getElementById('topmenu').innerHTML='$menu';";

echo "document.getElementById('messages').innerHTML='$message';";

if (W3DEBUG)  $debug=new WS3_Debug();

?>